/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newkfp;

import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import newkfp.util.JsfUtil;

/**
 *
 * @author labib.farag
 */
@ManagedBean(name = "loginController")
@SessionScoped
public class LoginController implements Serializable{
    
    
     @EJB
    private newkfp.EmployeeFacade ejbFacade;

    public EmployeeFacade getEjbFacade() {
        return ejbFacade;
    }
     
   transient private String userName;
   transient private String password;

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
       public String loginCheck(){
        String userName=getUserName();
        String pass=getPassword();
        String page="login";
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true); 
        
        Employee emp=getEjbFacade().checkUserLogin(userName,pass);
        if(emp != null){
            session.putValue("userName",emp.getUserName());
           session.putValue("empid",emp.getIdEmployee());
            session.putValue("name",emp.getName());
            session.putValue("limits", emp.getLimits());
            page="/bill/CustomerBill";
        }
        else{
                JsfUtil.addErrorMessage(ResourceBundle.getBundle("/Bundle").getString("InvalidLogin"));
           
            }
        return page;
    }
}
